import 'package:flutter/material.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/material/material_item.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/view/search/search_case_list_widget.dart';
import 'package:loxen/view/search/search_inspiration_list_widget.dart';
import 'package:loxen/view/search/search_material_list_widget.dart';
import 'package:loxen/view/search/search_knowledge_list_widget.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/search_result_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class SearchResultWidget extends StatelessWidget {
  final SearchType mType;
  final String data;
  final int studyCategory;

  var searchTitleMap = {
    SearchType.INSPIRATION: '灵感搜索结果',
    SearchType.MATERIAL: '素材搜索结果',
    SearchType.KNOWLEDGE: '知识搜索结果',
    SearchType.CASE: '应用搜索结果'
  };

  SearchResultWidget({Key key, this.mType, this.data, this.studyCategory}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print(studyCategory);
    print(mType);
    return ChangeNotifierProvider<SearchResultViewModel>(
      create: (_) {
        SearchResultViewModel model = SearchResultViewModel();
        model.loadData(studyCategory: studyCategory, type: mType, data: data);
        model.changeCurrentCategory(studyCategory);
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          child: Container(
            color: Theme.of(context).primaryColor,
            child: SafeArea(
              child: Row(
                children: <Widget>[
                  MySubassembly.getBackIcon(context),
                  Expanded(
                    child: Center(
                      child: Text(
                        searchTitleMap[mType],
                        style: TextStyle(fontSize: 32.sp, color: Colors.white),
                      ),
                    ),
                  ),
                  IconButton(icon: Text(''), onPressed: null)
                ],
              ),
            ),
          ),
          preferredSize: Size(double.infinity, 95.h),
        ),
        body: Selector<SearchResultViewModel, BaseState>(
            builder: (context, state, __) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.CONTENT) {
                if (mType == SearchType.INSPIRATION) {
                  PagewiseLoadController<InspirationInfo> controller =
                      PagewiseLoadController(
                          pageFuture: (page) => context
                              .read<SearchResultViewModel>()
                              .nextInspirationData(page),
                          pageSize: 5);
                  return SearchInspirationList(pageLoadController: controller);
                } else if (mType == SearchType.MATERIAL) {
                  PagewiseLoadController<MaterialItem> controller =
                      PagewiseLoadController(
                          pageFuture: (page) => context
                              .read<SearchResultViewModel>()
                              .nextMaterialData(page),
                          pageSize: 8);
                  return SearchMaterialListWidget(
                    controller: controller,
                  );
                } else if (mType == SearchType.KNOWLEDGE) {
                  PagewiseLoadController<StudyInfo> controller =
                      PagewiseLoadController(
                        pageFuture: (page) => context
                            .read<SearchResultViewModel>().nextStudyData(page),
                        pageSize: 5
                      );
                  return SearchKnowledgeListWidget(
                      pageLoadController: controller,
                  );
                } else if (mType == SearchType.CASE) {
                  PagewiseLoadController<StudyInfo> controller =
                  PagewiseLoadController(
                      pageFuture: (page) => context
                          .read<SearchResultViewModel>().nextStudyData(page),
                      pageSize: 5
                  );
                  return SearchCaseListWidget(
                    pageLoadController: controller,
                  );
                }
              }
              if (state == BaseState.FAIL) {
                return FailWidget();
              }
              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              return Text('text');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }
}
